package com.ygqh.baby.controller.admin;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ygqh.baby.ao.Message;
import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.ao.ResultSet;
import com.ygqh.baby.po.AdminPermission;
import com.ygqh.baby.po.AdminRole;
import com.ygqh.baby.service.AdminPermissionService;
import com.ygqh.baby.service.AdminRoleService;

import com.ygqh.baby.utils.JsonUtils;

@Controller
@RequestMapping("/admin/sysrole")
public class AdminRoleController extends BaseController<Object, java.lang.Long>{
	@Autowired
	private AdminRoleService adminRoleService;
	@Autowired
	private AdminPermissionService adminPermissionService;
	
	@RequestMapping("searchRole")
	@ResponseBody
	public Map<String, Object> searchRole(String q, QueryInfo queryInfo){
		ResultSet<AdminRole> resultSet = adminRoleService.search(queryInfo, q,null);
	   return resultSet.toDatatables(null);
	}
	@RequestMapping("getRoleList")
	@ResponseBody
	public List<AdminRole> getRoleList(){
		return adminRoleService.findBySupplierId(getSession().getSupplierId());
	}
	
	@RequestMapping("saveRole")
	@ResponseBody
	public Message saveRole(String role,Long[] permissionIds){
		AdminRole adminRole=JsonUtils.toObject(role, AdminRole.class);	
		adminRole.setCreateBy(getSession().getTrueName());	
		adminRole.setSupplierId(getSession().getSupplierId());
		adminRole.setCreateTime(new Date());
	    adminRoleService.save(adminRole,permissionIds);		
		return SUCCESS_MESSAGE;

	}
	@RequestMapping("updateRole")
	@ResponseBody
	public Message updateRole(String role,Long[] permissionIds){
		AdminRole adminRole=JsonUtils.toObject(role, AdminRole.class);	
		adminRole.setUpdateBy(getSession().getTrueName());
		adminRole.setUpdateTime(new Date());
	    adminRoleService.update(adminRole,permissionIds);		
		return SUCCESS_MESSAGE;

	}
	@RequestMapping("getRole")
	@ResponseBody
	public AdminRole getRole(Long id){
		return adminRoleService.findDetailById(id);

	}
	@RequestMapping("updateRoleStatus")
	@ResponseBody
	public Message updateRoleStatus(Long id,Boolean isEnabled){
		AdminRole adminRole=new AdminRole();
		adminRole.setId(id);
		adminRole.setIsEnabled(isEnabled);			
        adminRoleService.updateByPrimaryKeySelective(adminRole);
		return SUCCESS_MESSAGE;

	}

	@RequestMapping("getAllPermission")
	@ResponseBody
	public List<AdminPermission>  getAllPermission(){	
		
	  return adminPermissionService.find();
	}

	@RequestMapping("getPermissionByUser")
	@ResponseBody
	public List<AdminPermission>  getPermissionByUser(){	
		
	  return	getSession().getMenus();
	}
	

}
